/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*
* EasyGUI example - by Andrea Galimberti
*/

#include <afc/afc.h>
#include <afc/easygui.h>
#include <gtk/gtk.h>


/* this function will be called when the button named "b1" is clicked */

int dummy ( EasyGUI * eg, char * wname, char * event_name, NodeMaster * extra_params )
{
	void * g = NULL;
	int k = 0;

	fprintf ( stdout, "Hello world! - widget name: %s - event: %s\n", wname, event_name );

	if ( afc_nodemaster_len ( extra_params ) > 0 ) {
		g = ( void * ) afc_nodemaster_first ( extra_params );
		for ( k = 0; k < afc_nodemaster_len ( extra_params ); k++ ) {
			printf ( "param: %x\n", ( int ) g );
			g = ( void * ) afc_nodemaster_next ( extra_params );
		}
	}

	return ( 0 );
}

/* this function is called when the radio button named "rb1" is clicked */

int rbtoggle ( EasyGUI * eg, char * wname, char * event_name, NodeMaster * extra_params )
{
	GtkWidget * w = afc_easygui_widget_get_by_name ( eg, wname );
	GtkWidget * lab = afc_easygui_widget_get_by_name ( eg, "l1" );

	if ( GTK_TOGGLE_BUTTON ( w )->active )
		gtk_label_set_text ( GTK_LABEL ( lab ), "first button DOWN" );
	else
		gtk_label_set_text ( GTK_LABEL ( lab ), "first button UP" );

	return ( 0 );
}


/* let's start from here */

int main ( int argc, char * argv[] )
{

	/* the AFC object must be instantiated and passed as the only argument to
		 the EasyGUI constructor */

	AFC * afc = afc_new (); 
  EasyGUI * easygui = afc_easygui_new ( afc );
	int afc_res = 0;

  if ( easygui == NULL ) 
  {
    fprintf ( stderr, "Init of class EasyGUI failed.\n" );
    return ( 1 );
  }

	/* always remember to call this before doing anything */

	afc_easygui_init ( easygui );

	/* here follows our GUI. Note that double quotes in the GUI definition
		 must be eascaped (otherwise they'll close the C string and generate an error) */

	afc_easygui_set_gui ( easygui,
"(window \"EasyGUI test\")
(rows
 (menu main_menu
  /File NULL NULL <Branch>
  /File/Open NULL dummy <Item>
  /File/Other NULL NULL <Branch>
  /File/Other/Touch NULL NULL <Item>
  /Edit NULL NULL <Branch>
  /Edit/Cut NULL NULL <CheckItem>
  /Edit/Paste NULL NULL <Branch>
  /Edit/Paste/First NULL NULL <RadioItem>
  /Edit/Paste/Second NULL NULL /Edit/Paste/First
  /Help NULL NULL <LastBranch>
  \"/Help/Nothing to help\" NULL NULL <Item>
 )
 (eqcols
  (eqrows
   (radiobutton rb1 A \"first radiobutton of group A\" rbtoggle)
   (radiobutton rb2 A \"second rb of group A\")
  )
  (eqrows
   (label l1 this-is-the-first-label)
   (label l2 second-label)
  )
 )
 (eqcols
  (button b1 \"Click me!\" dummy)
  (button b2 TEXT=floppy ICON=3floppy_mount.xpm)
 )
 (checkbutton cb1 Hello!)
 (cols
  (label l_text \"insert text\")
  (entry testo 5)
 )
 (combo combobox null first second third 4 5 six)
 (eqcols
  (text memo true)
  (scrolledwindow ALWAYS AUTOMATIC
   (cols
    (eqrows
     (checkbutton NULL \"Very very very very long text to see horizontal scrolling\")
     (checkbutton NULL Hello!2)
     (checkbutton NULL Hello!3)
     (checkbutton NULL Hello!4)
     (checkbutton NULL Hello!5)
     (checkbutton NULL Hello!6)
     (checkbutton NULL Hello!7)
     (checkbutton NULL Hello!8)
     (checkbutton NULL Hello!9)
     (checkbutton NULL Hello!10)
     (checkbutton NULL Hello!11)
    )
    (eqrows
     (button NULL button1)
     (button NULL button2)
     (button NULL button3)
     (button NULL button4)
     (button NULL button5)
     (button NULL button6)
     (button NULL button7)
     (button NULL button8)
     (button NULL button9)
     (button NULL button10)
     (button NULL button11)
    )
   )
  )
 )
 (eqcols
   (image img_name AceKingCards.xpm)
   (scale s_name horz 15 0 100 1 10 true top 0)
   (clist nome_lista 2 30 dummy NULL SHOWTITLES TITLEDATA WIDTHDATA JUSTIFYDATA
          Object -Qty
          100 100
          < |
          Potato 123
          Onion|AceKingCards.xpm  2
          Computer 1
          |3floppy_mount.xpm 12
   )
 )
 (eqcols
  (scale s_nome vert 15 0 100 1 10 true left 0)
  (tree mytree NULL NULL NULL
   /First_item                        <Item>   NULL NULL NULL NULL NULL
   /Second_item                       <Branch> NULL NULL NULL NULL NULL
   /Second_item/First_sub             <Branch> dummy dummy dummy dummy dummy 
   /Second_item/First_sub/Item_lev3   <Item>   NULL NULL NULL NULL NULL
   /Second_item/First_sub/Item_lev3_2 <Item>   NULL NULL NULL NULL NULL
   /Second_item/Second_sub            <Item>   NULL NULL NULL NULL NULL
   /Third_item                        <Item>   NULL NULL NULL NULL NULL
  )
 )
)");

	/* show the gui and, if all is ok, call the main event cycle */

	if ( ( afc_res = afc_easygui_show ( easygui ) ) != AFC_EASYGUI_ERR_NO_ERROR ) {
		fprintf ( stderr, "res: %d\n", afc_res );
	}
	else afc_easygui_main ( easygui );

	/* we get here only when the user closes the window, so perform some cleanup and exit */

  afc_easygui_delete ( easygui );
	afc_delete ( afc );

  return ( 0 ); 
}

